package com.jmz.apt.api.configure;

import com.jmz.apt.api.Helper.ResponseHelper;
import com.jmz.apt.api.filter.AclFilter;
import com.jmz.apt.api.filter.LoginFilter;
import com.jmz.apt.api.service.LoginService;
import com.jmz.apt.api.service.auth.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.io.Serializable;

/**
 * Created by fanglexue on 2019/10/25.
 */
@Configuration
public class FilterRegistrationConfig implements WebMvcConfigurer {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    @Autowired
    Token token;



    @Bean
    public FilterRegistrationBean aclFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(aclFilter());
        registration.addUrlPatterns("/*");
        //registration.addInitParameter("paramName", "paramValue");
        registration.setName("acl");
        registration.setOrder(2);
        return registration;

    }

    @Bean
    public FilterRegistrationBean loginFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        ResponseHelper responseHelper = new ResponseHelper();
        LoginService loginService = new LoginService();
        registration.setFilter(new LoginFilter(stringRedisTemplate,redisTemplate, token,responseHelper,loginService));
        registration.addUrlPatterns("/*");
        //registration.addInitParameter("paramName", "paramValue");
        registration.setName("login");
        registration.setOrder(1);
        return registration;

    }


    @Bean
    public AclFilter aclFilter() {
        return new AclFilter();
    }
}
